/**
 * zmn产品模块-产品列表
 * @author tanghongjun
 * @since 2018/10/18
 */
layui.define(['index', 'form', 'table', 'laypage', 'layedit'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;
    var table = layui.table;
    var laypage = layui.laypage;
    var layedit = layui.layedit;

    table.init('zmn-table', {
        toolbar: "#zmn-table-tool",
        defaultToolbar: json.hasExportExcel ? ['filter', 'exports'] : ['filter'], done: function () {
            $("div[lay-event='LAYTABLE_EXPORT']").attr('lay-event', 'CUSTOM_EXPORT');
        },
        page: true,
        limit: json.pageSize,
        height: 'full-149'
    });
    laypage.render(layui.$.extend({limits: [10, 20, 30, 40, 50, 100]}, zmnPage.laypageOptions(json)));

    let ids = new Array();

    // 选择（全选或单选）
    table.on("checkbox(zmn-table)", function (obj) {
        ids = table.checkStatus('zmn-table').data.map((item) => item.groupId);
    });

    form.on('submit(query-export)', function (data) {
        let index = layer.load(1, {
            shade: [0.5, '#DBDBDB'] //0.1透明度的白色背景
        });
        zmnUtil.downloadLimitByConfig("/product/productgroup/outputExcel.action?" + $("#product_group_list").serialize(), "产品组报表.xlsx", function () {
            layer.close(index);
        });
        return false;
    });

    // 启用/停用
    table.on("toolbar(zmn-table)", function (obj) {
        switch (obj.event) {
            case 'CUSTOM_EXPORT':
                $("#query-export").click();
                return;
            case 'stop':
            case 'start':
                if (ids.length === 0) {
                    return layer.alert("请选择至少1个产品！");
                }
                zmnPage.layStartStop(obj, "/product/productgroup/updateStatus.action", ids.join(","));
            default:
                return;
        }
    });

    // 修改排序
    table.on("edit(zmn-table)", function (obj) {
        zmnPage.laySort(obj, "/product/productgroup/updateSort.action", obj.data.groupId);
    });

    //查询框联动
    form.on('select(bizTypeFilter)', function (data) {
        let bizType = data.value;
        let option = "<option value=\"\">-选择-</option>";
        $("#bizLine").html(option);
        $.ajax({
            async: false,
            type: "GET", url: "/product/businessline/listVtByBizType.action?bizType="+bizType,
            dataType: "json",
            success:function(result) {
                if(result.status == 200) {
                    let items = result.data;
                    items.forEach(item => {
                        option += "<option value='"+item.value+"'>"+item.text+"</option>";
                    });
                    $("#bizLine").html(option);
                } else {
                    layui.layer.msg(result.message,{"icon":2});
                }
                form.render("select");
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
            }
        });
        return false;
    });

    //对外暴露的接口
    exports('plat_productgroup_list', {});
});

function toaddProductGroup() {
    layer.open({
        type: 2,
        title: '新增产品组',
        area: ['650px', '330px'],
        content: '/product/productgroup/addEditView.action'
    });

}

function toEditProductGroup(groupId) {
    layer.open({
        type: 2,
        title: '修改产品组',
        area: ['650px', '380px'],
        content: '/product/productgroup/addEditView.action?groupId=' + groupId
    });

}

function toSetExamiUrl(groupId) {
    layer.open({
        type: 2,
        title: '考试配置',
        area: ['600px', '480px'],
        content: '/product/engineerexamineurl/addEditView.action?groupId=' + groupId
    });

}

function toSetReimbursementAmount(groupId) {
    layer.open({
        type: 2,
        title: '报销金额',
        area: ['600px', '250px'],
        content: '/product/productgroup/reimbursementAmount/addEditView.action?groupId=' + groupId
    });

}

function reload(msg, flag) {
    if (json.hasServCategAss && flag) {
        layer.open({
            type: 1
            , title: '提示'
            , shade: 0.8
            , area: ['400px', '180px']
            , btnAlign: 'c'
            , btn: ['前往', '取消']
            , content: '<div style="margin-top: 20px; margin-left: 20px">产品分组新建修改成功，是否前往对产品分类进行关联</div>'
            , yes: function () {
                parent.layui.index.openTabsPage('/serv/categ/ass/list.action', "后台分类关联");
            }, btn2: function (index, layero) {
                window.location.reload();
            }, cancel: function () {
                window.location.reload();
            }
        });
    } else {
        parent.layui.layer.msg(msg);
        window.location.reload();
    }
}
